//
//  TitleView.m
//  IntelligenceCommunity
//
//  Created by zhangpei on 15/12/23.
//  Copyright © 2015年 zhangpei. All rights reserved.
//

#import "TitleView.h"

@interface TitleView ()
{
    UIImageView *iconImage;
}

@end

@implementation TitleView

@synthesize titleLabel, titleButton;
/**
 *  初始化
 */
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initView];
    }
    return self;
}

- (void) setTitleViewTitle:(NSString *)title
{
    titleLabel.text = title;
    
    [titleLabel sizeToFit];
    
    CGRect oldTitleLabelFrame = titleLabel.frame;
    
    CGRect oldIconImageFrame = iconImage.frame;

    CGFloat x = (self.bounds.size.width - (oldTitleLabelFrame.size.width  + oldIconImageFrame.size.width))/2;
    
    titleLabel.frame = CGRectMake(x, (self.bounds.size.height - oldTitleLabelFrame.size.height)/2, oldTitleLabelFrame.size.width, oldTitleLabelFrame.size.height);
    
    iconImage.frame = CGRectMake(titleLabel.frame.origin.x+titleLabel.frame.size.width, oldIconImageFrame.origin.y, oldIconImageFrame.size.width, oldIconImageFrame.size.height);
}

/**
 *  创建View
 */
- (void)initView
{
    
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [self addSubview:titleLabel];
    
    
    UIImage *select_down = [UIImage imageNamed:@"select_down"];
    CGFloat imageViewHeight = 14;
    iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, (self.bounds.size.height - imageViewHeight)/2, imageViewHeight * select_down.size.width / select_down.size.height, imageViewHeight)];
    iconImage.image = select_down;
    iconImage.backgroundColor = [UIColor clearColor];
    [self addSubview:iconImage];
    
    titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    titleButton.backgroundColor = [UIColor clearColor];
    titleButton.frame = self.bounds;
    [self addSubview:titleButton];
}





@end
